from urllib.parse import unquote

import cx_Oracle
from django.db import connection
from django.http import HttpResponse
import json

from django.shortcuts import render

'''
if request.method == 'GET':
    username = request.GET.get('username')
    password = request.GET.get('password')
    term = request.GET.get('term')
elif request.method == 'POST':
    print("POST ...... ")
    data_json = json.loads(request.body)
    username = unquote(data_json.get('username'))
    password = unquote(data_json.get('password'))
    term = unquote(data_json.get('term'))
'''


def hello(request):
    username = 'null'
    password = 'null'
    if request.method == 'GET':
        print('GET...')
        username = request.GET.get('username')
        password = request.GET.get('password')
    elif request.method == 'POST':
        print('POST...')
        params = json.loads(request.body)
        username = unquote(params.get('username'))
        password = unquote(params.get('password'))
    print("username: %s,password: %s" % (username, password))
    return HttpResponse("Hello World!<br/>" + "username: %s,password: %s" % (username, password))


def runoob(request):
    content = {
        "title": "Hello World",
        "score": 25,
        "list": [
            {"id": 1, "name": "张三", "age": 19},
            {"id": 2, "name": "李四", "age": 20},
            {"id": 3, "name": "王五", "age": 21},
            {"id": 4, "name": "赵六", "age": 22},
        ],
        "val1": 'abc',
        "val2": "abc",
    }
    return render(request, 'runoob.html', content)


def test(request):
    cur = connection.cursor()
    cur.execute('select * from dual')
    resultSet = cur.fetchall()
    for res in resultSet:
        print(res)
    return HttpResponse('OK')
